package prickworlds.game.templates;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import prickworlds.graphics.FullTexture;
import prickworlds.graphics.Texture;
import prickworlds.util.Path;

public class RobotTemplate extends Template {
	/**
	 * the maximum speed the robot can be moving
	 */
	float maxSpeed;

	int maxHp;
	
	int level = 1;

	Texture lowerTexture;
	Texture upperTexture;

	WeaponTemplate[] standardWeapons;

	public RobotTemplate() {
		lowerTexture = FullTexture.createTest();
		upperTexture = FullTexture.createTest();
	}

	public float getMaxSpeed() {
		return maxSpeed;
	}

	public int getMaxHp() {
		return maxHp;
	}

	public Texture getLowerTexture() {
		return lowerTexture;
	}

	public Texture getUpperTexture() {
		return upperTexture;
	}

	public static RobotTemplate loadRobotTemplate(String path,
			MapTemplate mapTemplate) {
		RobotTemplate rt = new RobotTemplate();

		Element e = Template.createElement(path);

		try {
			try {
				Element maxSpeed = (Element) e.getElementsByTagName("maxSpeed")
						.item(0);
				rt.maxSpeed = Float.parseFloat(maxSpeed.getAttribute("value"));
			} catch (RuntimeException ex) {
				ex.printStackTrace();
			}

			try {
				Element maxHp = (Element) e.getElementsByTagName("maxHp").item(
						0);
				rt.maxHp = Integer.parseInt(maxHp.getAttribute("value"));
			} catch (RuntimeException ex) {
				ex.printStackTrace();
			}

			try {
				Element topImage = (Element) e.getElementsByTagName("topImage")
						.item(0);
				Path p = new Path(path);
				p.removeLast();
				p.add(new Path(topImage.getAttribute("path")));
				FileInputStream imgIn = new FileInputStream(new File(
						p.getString()));
				rt.upperTexture = FullTexture.loadImage(imgIn);
			} catch (RuntimeException ex) {
				ex.printStackTrace();
			} catch (FileNotFoundException fnfe) {
				fnfe.printStackTrace();
			}

			try {
				Element bottomImage = (Element) e.getElementsByTagName(
						"bottomImage").item(0);
				Path p = new Path(path);
				p.removeLast();
				p.add(new Path(bottomImage.getAttribute("path")));
				FileInputStream imgIn = new FileInputStream(new File(
						p.getString()));
				rt.lowerTexture = FullTexture.loadImage(imgIn);
			} catch (RuntimeException ex) {
				ex.printStackTrace();
			} catch (FileNotFoundException fnfe) {
				fnfe.printStackTrace();
			}

			try {
				Element weap = (Element) e.getElementsByTagName("weapons")
						.item(0);

				NodeList weapons = weap.getElementsByTagName("weapon");
				rt.standardWeapons = new WeaponTemplate[weapons.getLength()];

				for (int i = 0; i < weapons.getLength(); i++) {
					Element weapon = (Element) weapons.item(i);
					rt.standardWeapons[i] = mapTemplate.getWeaponByName(weapon
							.getAttribute("name"));
				}
			} catch (RuntimeException ex) {
				ex.printStackTrace();
			}

		} catch (RuntimeException ex) {
			ex.printStackTrace();
		}
		return rt;
	}

	public void setMaxHp(int i) {
		maxHp = i;
	}

	public WeaponTemplate[] getStandardWeapons() {
		return standardWeapons;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}
}
